package com.dreamoo.common.threadpool.factory;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 山顶洞人郑某
 * @className DreamooThreadPoolNameFactory.java 线程池名称工厂类
 * @date 2025 05 06
 */
public class DreamooThreadPoolNameFactory implements ThreadFactory {
    
    /**
     * 线程组
     */
    private final ThreadGroup group;
    
    /**
     * 线程计数器
     */
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    
    /**
     * 线程名称前缀
     */
    private final String namePrefix;
    
    /**
     * 是否为守护线程
     */
    private final boolean daemon;
    
    /**
     * 线程优先级
     */
    private final int priority;
    
    /**
     * 构造方法
     * @param namePrefix 线程名称前缀
     */
    public DreamooThreadPoolNameFactory(String namePrefix) {
        this(namePrefix, false, Thread.NORM_PRIORITY);
    }
    
    /**
     * 构造方法
     * @param namePrefix 线程名称前缀
     * @param daemon 是否为守护线程
     */
    public DreamooThreadPoolNameFactory(String namePrefix, boolean daemon) {
        this(namePrefix, daemon, Thread.NORM_PRIORITY);
    }
    
    /**
     * 构造方法
     * @param namePrefix 线程名称前缀
     * @param daemon 是否为守护线程
     * @param priority 线程优先级
     */
    public DreamooThreadPoolNameFactory(String namePrefix, boolean daemon, int priority) {
        SecurityManager s = System.getSecurityManager();
        this.group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
        this.namePrefix = namePrefix;
        this.daemon = daemon;
        this.priority = priority;
    }
    
    @Override
    public Thread newThread(Runnable r) {
        // 创建线程并命名
        Thread t = new Thread(group, r, namePrefix + "-thread-" + threadNumber.getAndIncrement(), 0);
        // 设置是否为守护线程
        t.setDaemon(daemon);
        // 设置线程优先级
        if (priority != Thread.NORM_PRIORITY) {
            t.setPriority(priority);
        }
        return t;
    }
    
    /**
     * 创建常用线程池名称工厂
     */
    public static class Builder {
        /**
         * 创建梦境处理线程池名称工厂
         * @return 线程工厂
         */
        public static ThreadFactory dreamProcessorThreadFactory() {
            return new DreamooThreadPoolNameFactory("dream-processor");
        }
        
        /**
         * 创建睡眠数据分析线程池名称工厂
         * @return 线程工厂
         */
        public static ThreadFactory sleepDataAnalysisThreadFactory() {
            return new DreamooThreadPoolNameFactory("sleep-data-analysis");
        }
        
        /**
         * 创建用户通知线程池名称工厂
         * @return 线程工厂
         */
        public static ThreadFactory userNotificationThreadFactory() {
            return new DreamooThreadPoolNameFactory("user-notification");
        }
        
        /**
         * 创建后台任务线程池名称工厂
         * @return 线程工厂
         */
        public static ThreadFactory backgroundTaskThreadFactory() {
            return new DreamooThreadPoolNameFactory("background-task", true);
        }
        
        /**
         * 创建自定义名称线程池名称工厂
         * @param namePrefix 名称前缀
         * @return 线程工厂
         */
        public static ThreadFactory customThreadFactory(String namePrefix) {
            return new DreamooThreadPoolNameFactory(namePrefix);
        }
    }
} 